﻿using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace UFrame
{
    public class SkillListWindow : SkillSecondWindow
    {
        private List<SkillListButtonData> data = new List<SkillListButtonData>();
        private string id = "";
        private string tempName = "";
        private string curName = "";
        public bool isHigh = false;

        public SkillListWindow(EffectEditor t, float x, float y, float width, float height) : base(t, x, y, width, height)
        {
            name = "技能列表";
        }

        protected override void Draw(int id)
        {
            if (isHigh)
            {
                name = "高配技能列表";
            }
            else
            {
                name = "低配技能列表";
            }

            GUILayout.BeginVertical();
            GUILayout.BeginHorizontal();
            EditorGUI.BeginChangeCheck();



            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();

            tempName = GUILayout.TextField(tempName);

            if (GUILayout.Button("重命名"))
            {
                if (tempName == string.Empty)
                {
                    if (EditorUtility.DisplayDialog("", "不能使用空名称", "确定"))
                    {
                        return;
                    }
                }
                else if (tempName == curName)
                {
                    return;
                }
                else
                {
                    /*SkillEffectInfo info = null;
                    if (root.skillData.data.ContainsKey(tempName))
                    {                                                
                        info = root.skillData.data[tempName];
                        root.skillData.data.Remove(tempName);
                    }
                    else
                    {
                        if (!root.mskillData.data.ContainsKey(curName))
                        {
                            if (EditorUtility.DisplayDialog("", "已经失效的配置项", "确定"))
                            {
                                datas.idHighList.Remove(curName);
                                return;
                            }                            
                        }
                        
                        info = root.skillData.data[curName];
                    }                                        

                    if (isHigh)
                        datas.idHighList.Remove(curName);
                    else
                        datas.idLowList.Remove(curName);
                    
                    root.skillData.data.Remove(curName);
                    
                    if (isHigh)
                        datas.idHighList.Add(tempName);
                    else
                        datas.idLowList.Add(tempName);
                    root.skillData.mdata.Add(tempName, info);
                    info.name = tempName;
                    return;*/
                }
            }

            GUILayout.EndHorizontal();


            /*            if (datas != null)
                        {
                            List<string> list = null;
                            if (isHigh)
                            {
                                list = datas.idHighList;
                            }
                            else
                            {
                                list = datas.idLowList;
                            }
                            foreach (var data in list)
                            {
                                GUILayout.BeginHorizontal();
                                if (GUILayout.Button(data.ToString()))
                                {
                                    SkillEffectEvent ev = new SkillEffectEvent()
                                    {
                                        eventT = SkillEffectEventEnum.SkillSelected,
                                        obj = data,
                                    };

                                    root.OnEvent(ev);
                                    tempName = data;
                                    curName = data;

                                }

                                if (GUILayout.Button("删除"))
                                {
                                    root.OnEvent(new SkillEffectEvent() { eventT = SkillEffectEventEnum.SkillRemove, object = data });
                                    if (isHigh)
                                        datas.idHighList.Remove(data);
                                    else
                                        datas.idLowList.Remove(data);
                                    root.skillData.data.Remove(data);

                                    return;
                                }


                                if (GUILayout.Button("套用"))
                                {
                                    EffectCopySelectWindow window = EditorWindow.GetWindowWithRect<EffectCopySelectWindow>(new Rect(0f, 0f, 200f, 40f), false, "选择要套用的配置", true);
                                    window.Init(datas.idHighList, (content) =>
                                    {
                                        if (root.skillData.data.ContainsKey(data) && root.skillData.data.ContainsKey(content))
                                        {
                                            var info = root.skillData.data[content].Clone() as SkillEffectInfo;
                                            info.name = data;
                                            root.skillData.data[data] = info;
                                        }                           
                                    });                                               
                                }

                                GUILayout.EndHorizontal();
                            }
                        }*/


            GUILayout.EndVertical();
        }

        public override void OnEditorEvent(IEditorEvent e)
        {
            switch ((SkillEffectEventEnum)e.GetEventID())
            {
                case SkillEffectEventEnum.ChangeObj:
                    {
                        RefreshList();
                        break;
                    }
            }
        }

        private void RefreshList()
        {
            id = "";
            tempName = "";
            string name = GetObjectName();
            //data = root.config.Find(name);
            //data.Clear();
        }

        private string GetObjectName()
        {
            string path = AssetDatabase.GetAssetPath(root.obj);

            if (path.Contains("Assets/Res/Character/"))
            {
                ObjectType type;
                path = path.Replace("Assets/Res/Character/", "");

                if (path.StartsWith("Monster"))
                {
                    return root.obj.name;
                }
                else if (path.StartsWith("Boss"))
                {
                    return root.obj.name;
                }
                else if (path.StartsWith("Role"))
                {
                    string[] strs = path.Split(new string[] { "zhujue_" }, StringSplitOptions.None);
                    string name = strs[strs.Length - 1];
                    if (name.EndsWith(".prefab"))
                    {
                        name = name.Replace(".prefab", "");
                    }
                    strs = name.Split('_');
                    name = string.Format("{0}_{1}", strs[0], strs[1]);

                    return name;
                }
                else if (path.StartsWith("Pet"))
                {
                    return root.obj.name;
                }


            }
            return "";
        }
    }

    public class SkillListButtonData
    {
        public string name;
        public Action action;
    }
}
